package com.example.cinema.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.example.cinema.R;

import java.util.List;
//通用适配器
public class UnifiedWorksAdapter<T>  extends BaseAdapter {

    private List<T> data;
    private LayoutInflater inflater;
    private int layoutid;
    private int brid;

    public UnifiedWorksAdapter(List<T> data, int layoutid, int brid, Context context) {
        this.data = data;
        this.layoutid = layoutid;
        this.brid = brid;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

//    private ViewDataBinding dataBinding;
    private FuYong fu;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null)
        {
            ViewDataBinding dataBinding = DataBindingUtil.inflate(inflater,layoutid,parent,false);
            convertView = dataBinding.getRoot();
            fu = new FuYong();
            fu.dataBinding = dataBinding;
            convertView.setTag(fu);
        }
        FuYong fuYong = (FuYong) convertView.getTag();
        fuYong.dataBinding.setVariable(brid,data.get(position));
        return convertView;
    }

    class FuYong
    {
        public ViewDataBinding dataBinding;
    }
}
